package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Clue;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController()
@RequestMapping("/clues")
public class ClueController {
    @Autowired
    private ClueService clueService;
    //新增线索
    @PostMapping
    public Result addClue(@RequestBody Clue clue) {
        log.info("新增线索");
        clue.setStatus(1);
        //添加创建时间
        clue.setCreateTime(LocalDateTime.now());
        //添加更新时间
        clue.setUpdateTime(LocalDateTime.now());
        //保存数据
        clueService.save(clue);
        //返回数据
        log.info("保存数据：" + clue);
        return Result.success();
    }
//列表查询
    @GetMapping
    public Result listClues(ClueDto clueDto) {
        log.info("列表查询");
        //调用service方法
        PageResult<Clue> pageResult = clueService.listClues(clueDto);
        //返回数据
        return Result.success(pageResult);
    }
//分配线索跟进人
    @PutMapping("/assign/{clueId}/{userId}")
    public Result assignClue(@PathVariable Integer clueId, @PathVariable Integer userId) {
        log.info("分配线索跟进人");
        //查询线索是否存在
        Clue clue = new Clue();

        clue.setId(clueId);
        //设置status
        clue.setStatus(2);
        //设置归属人
        clue.setUserId(userId);
        //设置更新时间
        clue.setUpdateTime(LocalDateTime.now());
        //保存更新后的线索信息
        clueService.updateById(clue);
        //返回数据
        return Result.success();
    }
//根据id查询线索详细信息
    @GetMapping("/{id}")
    public Result getClueById(@PathVariable Integer id) {
        log.info("根据id查询线索详细信息");
        //调用service方法
        Clue clue = clueService.getClueById(id);
        //返回数据
        return Result.success(clue);
    }
    //跟进线索
    @PutMapping
    public Result trackClue(@RequestBody Clue clue) {
        log.info("跟进线索");
        clueService.trackClue(clue);
        return Result.success();

    }
    //伪线索处理
    @PutMapping("/false/{id}")
    public Result handleClue(@PathVariable Integer id, @RequestBody MarkFalseClueDto markFalseClueDto) {
        log.info("伪线索处理");
        clueService.handleClue(id,markFalseClueDto);
        //返回数据
        return Result.success();
    }
//转商机处理
    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id) {
        log.info("转商机处理");
        //调用service方法
        clueService.toBusiness(id);
        //返回数据
        return Result.success();
    }


}
